package com.force.jianghuling.httputils;

import android.content.Context;

import com.force.jianghuling.entity.User;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 网络请求方法
 * Created by zzj on 2016/12/3.
 */
public class HttpUtils {


    public static final String PUBLIC_URL = "https://api.leancloud.cn/1.1";
    /**
     * 注册网路请求：
     */
    public static void register(Context context, final DataListener dataListener, String url, String username, String password, String phone){
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        params.put("phone", phone);
        JSONObject jsonObject = new JSONObject(params);
        OkGo.post(url)
                .tag(context)
                .upJson(jsonObject)
                .execute(new StringCallback() {


                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s!=null){
                            dataListener.onSuccess(s,"register");
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        dataListener.onFail(e,"register");
                    }
                });
    }
    /**
     * 注册网路请求：
     */
    public static void login(Context context, final DataListener dataListener, String url, String username, String password){
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        JSONObject jsonObject = new JSONObject(params);
        OkGo.post(url)
                .tag(context)
                .upJson(jsonObject)
                .execute(new StringCallback() {


                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s!=null){
                            dataListener.onSuccess(s,"login");
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        dataListener.onFail(e,"login");
                    }
                });
    }
    /**
     * 签到网路请求：
     */
    public static void signs(Context context, final DataListener dataListener, String username){
        HashMap<String, String> params = new HashMap<>();
        params.put("content", username);
        JSONObject jsonObject = new JSONObject(params);
        OkGo.post(PUBLIC_URL+"/classes/Signs")
                .tag(context)
                .upJson(jsonObject)
                .execute(new StringCallback() {


                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s!=null){
                            dataListener.onSuccess(s,"signs");
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        dataListener.onFail(e,"signs");
                    }
                });
    }
}
